package com.quick.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MapUtil {

	public <T> List<T> getKey(Map<T, ?> map) {
		List<T> objects = new ArrayList<T>();
		for (T key:map.keySet()) {
			objects.add(key);
		}
		return objects;
	}

    public static Map<String, Object> toMap(Object value) {
        Map<String,Object> map = new HashMap<>();
        Field[] fields = value.getClass().getDeclaredFields();
        for (Field field : fields) {
            try {
                field.setAccessible(true);
                map.put(field.getName(), String.valueOf(field.get(value)));
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        return map;
    }
    
    public static void toBean(Map<String, Object> map, Object target){
        if (map == null) {
            return;
        }
        Class<?> cls = target.getClass();
        for (Field field : cls.getDeclaredFields()) {
            String name = field.getName();
            Object value = map.get(name);
            if (value != null) {
                field.setAccessible(true);
                try {
					field.set(map, value);
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				}
            }
        }
    }

}
